"""
Config for log2bq
"""
import os

from google.appengine.api import lib_config, servers

APP_ID_FOR_BQ_GS = os.environ.get('APPLICATION_ID', 'LOCALHOST').lower().strip('s~').replace('-', '_')

class ConfigDefaults(object):
    """
    Configuration defaults.

    To override any of these values, add entries into your appengine_config.py file, prefixed with 'log2bq_', e.g.,

        log2bq_BQ_PROJECT_ID = '12345679'
        log2bq_generate_tags = some_function_pointer
    """

    # The name of the queue to use for the pipeline.
    QUEUE = 'default'

    # The BigQuery Google APIs console project ID to use
    BQ_PROJECT_ID = 'TODO-ADD-PROJECT-ID'

    # The BigQuery Dataset to drive the logs in to. Must be manually created in BigQuery console
    # Default is your app id, with dashes changed to underscores
    BQ_DATASET = APP_ID_FOR_BQ_GS

    # The prefix for the log tables in BigQuery. The date (e.g., '20130321') will be appended.
    # NOTE: It is vital that the prefix is unique within the dataset because the scrubbing process
    # will drop old tables that start with this prefix.
    BQ_TABLE_PREFIX = 'log2bq_'

    # The Cloud Storage (GoogleStorage) bucket that will be used to stage the log files.
    # The bucket must be manually created and the App Engine application service account
    # must have write access to it. Default is app_id, with dashes changed to underscores
    # and appended with '_logs'.
    GS_BUCKET_NAME = APP_ID_FOR_BQ_GS + '_logs'

    # The prefix to use on the GS uploaded files. Defaults to 'log2bq-' + your app id, with dashed
    # changed to underscores.
    GS_FILE_PREFIX = 'log2bq-' + APP_ID_FOR_BQ_GS

    # The age of the log tables to keep, in days on BigQuery. Set to None
    # to never scrub tables.
    SCRUB_AGE = 10

    # Includes the application logs in the request log. Typically, you would set this to
    # True if you want to look at the application logs when calling generate_tags.
    INCLUDE_APP_LOGS = False

    def generate_tags(request_log):
        """
        Can be used to generate tags from the logs, e.g., to track specific accounts.
        Must return a list of strings.

        Args:
            request_log: The RequestLog (see https://developers.google.com/appengine/docs/python/logs/requestlogclass)
        """
        return []

    def get_versions():
        """
        Used to determine which specific versions to grab logs for. Default is all
        versions (note that only the current hour's worth of logs are grabbed from
        each, so non-default versions will typically have little to no entries).
        """
        # TODO: the following line requires a trusted tester to be enabled, so it
        # doesn't work right now (2013-03-22). Instead, we just get the current app version
        # (which is likely the default version anyway).
        # versions = servers.get_versions() # TODO: re-enable this
        major, minor = os.environ["CURRENT_VERSION_ID"].split(".")
        return [major]

# registers config to app engine, this also updates it.
config = lib_config.register('log2bq', ConfigDefaults.__dict__)
